//
// GPSMapEdit
// (c) Konstantin Galichsky (kg@geopainting.com), 2007
//
// 'Options' dialog, 'Load & Save' page.
//

# include "StdAfx.h"
# include "Resource.h"
# include "Options.h"
# include "FileTypeInfo.h"
# include "Settings.h"
# include "Menu.h"
# include "PropertyPages.h"
# include "ListView.h"
# include "Globals.h"

enum {
	lvName   = 0,
	lvExt    = 1,
	lvMethod = 2,
	lvClass  = 3,
};

INT_PTR CALLBACK _OptionsFileProc (HWND _hWnd, UINT _uMsg, WPARAM _wParam, LPARAM _lParam) {
	switch (_uMsg) {
		case WM_INITDIALOG: {
			// Zoom to Full Map after 'File | Add'.
			::CheckDlgButton (_hWnd, IDC_ZOOM_TO_FULL_AFTER_ADD, g_bZoomToFullAfterAdd ? BST_CHECKED : BST_UNCHECKED);

			// Formats.
			const HWND hFormats = ::GetDlgItem (_hWnd, IDC_FORMATS);

			ListView_SetExtendedListViewStyleEx (hFormats, LVS_EX_FULLROWSELECT | LVS_EX_CHECKBOXES, LVS_EX_FULLROWSELECT | LVS_EX_CHECKBOXES);

			AddColumn (hFormats, lvName,   "Name");
			AddColumn (hFormats, lvExt,    "Extension");
			AddColumn (hFormats, lvMethod, "Method");
			AddColumn (hFormats, lvClass,  "Class");

			size_t cItem = 0;
			for (const CFileTypeInfo * p = CFileTypeInfo::s_pFirst; p; p = p->m_pNext) {
				if (p->m_pfCreateSaver == NULL)
					continue;

				string_t strExt;
				const size_t cExts = p->m_vecExts.size ();
				for (size_t c = 0; c < cExts; ++ c) {
					if (! strExt.empty ())
						strExt.append (";");
					strExt.append (p->m_vecExts [c]);
				}

				InsertItem     (hFormats, cItem, reinterpret_cast<LPARAM> (p), "%s", p->m_strFilter);
				SetSubItemText (hFormats, cItem, lvExt,    "%s", strExt.c_str ());
				SetSubItemText (hFormats, cItem, lvMethod, (p->m_dwFlags & ftiImportExport) != 0 ? "Export" : "Save");
				SetSubItemText (hFormats, cItem, lvClass,  "%s", p->m_strClass);

				ListView_SetCheckState (hFormats, cItem, p->IsSaveEnabled ());

				++ cItem;
			}

			AutoSizeColumns (hFormats);
			
			return TRUE;
		}

		case WM_COMMAND:
			switch (LOWORD (_wParam)) {
				case IDC_ZOOM_TO_FULL_AFTER_ADD:
					PropSheet_Changed (::GetParent (_hWnd), _hWnd);
					return TRUE;

				case IDC_ENABLE_ALL: {
					const HWND hFormats = ::GetDlgItem (_hWnd, IDC_FORMATS);
					const size_t cFormats = ListView_GetItemCount (hFormats);
					for (size_t c = 0; c < cFormats; ++ c)
						ListView_SetCheckState (hFormats, c, TRUE);
					return TRUE;
				}
			}
			break;

		case WM_NOTIFY: {
			const NMHDR * const pInfo = reinterpret_cast<const NMHDR *> (_lParam);
			switch (pInfo->code) {
				case PSN_APPLY: {
					// Zoom to Full Map after 'File | Add'.
					g_bZoomToFullAfterAdd = ::IsDlgButtonChecked (_hWnd, IDC_ZOOM_TO_FULL_AFTER_ADD) == BST_CHECKED;
					SaveSetting ("ZoomToFullAfterAdd", g_bZoomToFullAfterAdd);

					// Formats.
					const HWND hFormats = ::GetDlgItem (_hWnd, IDC_FORMATS);
					const size_t cFormats = ListView_GetItemCount (hFormats);
					bool bUpdateMenu = false;
					for (size_t c = 0; c < cFormats; ++ c) {
						CFileTypeInfo * const p = reinterpret_cast<CFileTypeInfo *> (GetLParam (hFormats, c));
						const bool bEnableSave = ListView_GetCheckState (hFormats, c) != FALSE;
						if (p->IsSaveEnabled () != bEnableSave) {
							p->EnableSave (bEnableSave);
							bUpdateMenu = true;
						}
					}

					if (bUpdateMenu)
						UpdateImportExportMenu ();

					break;
				}
			}
		}
	}

	return FALSE;
}
